/********************************************
 * 功能说明: Web端ProjectDatasource基础业务控制器
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.controller;

import com.codahale.metrics.annotation.Timed;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.uaa.dto.ProjectDatasourceDTO;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.dto.ProjectDatasourceDTO;
import com.spring.arch.uaa.model.ProjectDatasource;
import com.spring.arch.uaa.repository.ProjectDatasourceRepository;
import com.spring.arch.uaa.repository.ProjectRepository;
import com.spring.arch.uaa.service.ProjectDatasourceService;
import com.spring.arch.uaa.web.converter.WebProjectDatasourceConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001002001;
import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001007001;


/**
 * Web端ProjectDatasource基础业务控制器
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Slf4j
@RestController
@RequestMapping(value = "/uaa/w/projectDatasource", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = {"Web端ProjectDatasource管理API" })
public class WebProjectDatasourceController {

    @Autowired
    private ProjectDatasourceService projectDatasourceService;
    @Autowired
    private ProjectDatasourceRepository projectDatasourceRepository;
    @Autowired
    private WebProjectDatasourceConverter projectDatasourceConverter;

    /**
     * 新建操作
     *
     * @param projectDatasourceDTO 新建资源的DTO
     * @return 新建资源
     */
    @Timed
    @ApiOperation(value = "新建操作")
    @PostMapping
    public ResultDTO<ProjectDatasourceDTO> create(@RequestBody @Valid final ProjectDatasourceDTO projectDatasourceDTO) {
        final ProjectDatasource model = this.projectDatasourceConverter.toModel(projectDatasourceDTO);
        this.projectDatasourceService.save(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", ProjectDatasource.class.getSimpleName(), model.getId());
        }
        return this.projectDatasourceConverter.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param id 实体对象ID
     * @return 操作结果
     */
    @Timed
    @ApiOperation(value = "删除操作")
    @DeleteMapping(value = "/{id}")
    public ResultDTO<Void> delete(@PathVariable final UUID id) {
        this.projectDatasourceService.deleteById(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was disable.", ProjectDatasource.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    /**
     * 更新操作
     *
     * @param schema 实体对象ID
     * @param projectDatasourceDTO 更新资源的DTO
     * @return 更新后资源
     */
    @Timed
    @ApiOperation(value = "更新操作")
    @PutMapping(value = "/{schema}")
    public ResultDTO<ProjectDatasourceDTO> update(@PathVariable final String schema, @RequestBody @Valid final ProjectDatasourceDTO projectDatasourceDTO) {
        //现根据code查询数据源，code唯一
        if (!this.projectDatasourceRepository.existsBySchema(schema)) {
            throw ExceptionHolder.serviceException(UaaErrorCode.E2001007001, schema);
        }
        //查询对应数据源，进行更新
        ProjectDatasource projectDatasource = this.projectDatasourceRepository.findBySchema(schema);
        projectDatasource.setUrl(projectDatasourceDTO.getUrl());
        this.projectDatasourceRepository.save(projectDatasource);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", ProjectDatasource.class.getSimpleName(), projectDatasource.getId());
        }
        return this.projectDatasourceConverter.toResultDTO(projectDatasource);
    }

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @Timed
    @ApiOperation(value = "获取分页数据")
    @PostMapping(value = "/s")
    public PageResultDTO<ProjectDatasourceDTO> search(final Pageable pageable) {
        final Page<ProjectDatasource> models = this.projectDatasourceRepository.findAll(pageable);
        return this.projectDatasourceConverter.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "获取详细数据")
    @GetMapping(value = "/{id}")
    public ResultDTO<ProjectDatasourceDTO> get(@PathVariable final UUID id) {
        final ProjectDatasource model = this.projectDatasourceRepository.findById(id)
                    .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001002001, ProjectDatasource.class.getSimpleName()));
        return this.projectDatasourceConverter.toResultDTO(model);
    }
}
